import 'package:flutter/material.dart';
import 'package:pp_uikit/pp_group/pp_group_util.dart';
import 'package:pp_uikit/theme/pp_group_theme.dart';

///
/// 可组合title类型录入项
///
/// 包括"标题"、"副标题"、"必填项提示"
///
// ignore: must_be_immutable
class PPGroupTitle extends StatefulWidget {
  /// 标题
  final String title;

  /// 标题左边自定义View
  final Widget? titleWidget;

  /// 子标题
  final String? subTitle;

  /// 是否必填项
  final bool isRequire;

  /// 是否显示右边Icon
  final bool isShowRightIcon;

  /// 右边箭头方向
  final PPGroupArrawDirection arrawDirection;

  /// 子标题文本方向是否居右
  final bool isTextAlignRight;

  /// 子标题Widget
  final Widget? subTitleWidget;

  /// 点击回调
  final VoidCallback? onTap;

  final double minHeight;

  /// form配置
  PPGroupTheme? themeData;

  PPGroupTitle({
    Key? key,
    this.title = "",
    this.subTitle,
    this.isRequire = false,
    this.isShowRightIcon = true,
    this.arrawDirection = PPGroupArrawDirection.right,
    this.isTextAlignRight = true,
    this.titleWidget,
    this.subTitleWidget,
    this.onTap,
    this.minHeight = 40,
    this.themeData,
  }) : super(key: key) {
    themeData ??= PPGroupTheme();
  }

  @override
  State<StatefulWidget> createState() {
    return PPGroupTitleState();
  }
}

class PPGroupTitleState extends State<PPGroupTitle> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: widget.themeData?.backgroundColor,
      padding: widget.themeData?.groupPadding,
      child: ConstrainedBox(
        constraints: BoxConstraints(
          minHeight: widget.minHeight,
        ),
        child: GestureDetector(
          onTap: widget.onTap,
          behavior: HitTestBehavior.opaque,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: widget.themeData!.crossAxisAlignment,
            children: <Widget>[
              if (widget.titleWidget != null)
                Padding(
                  padding: EdgeInsets.only(right: 8),
                  child: widget.titleWidget!,
                ),

              // 必填项符号
              PPGroupUtil.buildRequireWidget(widget.isRequire),

              // 主标题
              PPGroupUtil.buildTitleWidget(widget.title,
                  style: widget.themeData?.titleTextStyle),

              Expanded(
                flex: 1,
                child: getSubTitleWidget(),
              ),

              // 显示右边Icon
              PPGroupUtil.buildRightIconWidget(widget.isShowRightIcon,
                  arrawDirection: widget.arrawDirection),
            ],
          ),
        ),
      ),
    );
  }

  Widget getSubTitleWidget() {
    if (widget.subTitleWidget != null) {
      return Container(
        child: widget.subTitleWidget,
      );
    } else {
      return Container(
        padding: const EdgeInsets.only(left: 10),
        child: Text(
          widget.subTitle ?? "",
          textAlign: widget.isTextAlignRight ? TextAlign.right : TextAlign.left,
          style: widget.themeData?.subTitleTextStyle,
        ),
      );
    }
  }
}
